package com.hex.liyue.giftmodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.utils.T;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.giftmodule.adapter.AdapterMyGitfStore;
import com.hex.liyue.launchdatemodule.activity.LaunchDateActivity;
import com.hex.liyue.storemodule.activity.StoreDetailsActivity;
import com.hex.liyue.storemodule.bean.FragmentStoreBean;
import com.hex.liyue.storemodule.presenter.FragmentStorePresenter;
import com.hex.liyue.storemodule.presenter.IFragmentStoreView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by $cmm on 2016/7/1.
 * 我的礼物界面跳转到商城界面
 */
public class MyGiftsStoresActivity extends BaseActivity implements AdapterMyGitfStore.OnItemClickListener, IFragmentStoreView, YRecycleview.RefreshAndLoadMoreListener {

    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.rv_store)
    YRecycleview rvStore;
    @Bind(R.id.ll_all)
    LinearLayout ll_all;

    private FragmentStorePresenter fragmentStorePresenter;
    private List<FragmentStoreBean.DataBean> list = new ArrayList<>();
    private AdapterMyGitfStore adapterMyGitfStore;
    private boolean sortPrice = true;//价格从高到低排
    private int page = 1, page_num = 10;
    private String flag;


    @Override
    protected void bindPresenter() {
        fragmentStorePresenter = new FragmentStorePresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {
        fragmentStorePresenter.getStoreList(page, page_num, sortPrice);
        flag = getIntent().getStringExtra("flag");
        if ("flag".equals(flag)) {
            ll_all.setVisibility(View.VISIBLE);
        } else {
            ll_all.setVisibility(View.GONE);
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_myorder_store;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        if ("flag".equals(flag)) {
            tvToobarTitle.setText("商城");
        } else {
            tvToobarTitle.setText("选择礼物");
        }
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarRight.setVisibility(View.VISIBLE);
        if (sortPrice) {
            tvToobarRight.setText("价格从高到低");
        } else {
            tvToobarRight.setText("价格从低到高");
        }
    }

    @Override
    public void setAdapter(FragmentStoreBean fragmentStoreBean) {
        if (page == 1) {
            list.clear();
        }
        if (fragmentStoreBean != null) {
            if (fragmentStoreBean.data.size() == 0 && page > 1) {
                page--;
            }
            for (FragmentStoreBean.DataBean dataBean : fragmentStoreBean.data) {
                list.add(dataBean);
            }
        }
        if (adapterMyGitfStore == null) {
            adapterMyGitfStore = new AdapterMyGitfStore();
            adapterMyGitfStore.setFlag(flag);
            adapterMyGitfStore.setList(list);
            adapterMyGitfStore.setOnItemClickListener(this);
            rvStore.setLayoutManager(new LinearLayoutManager(this));
            rvStore.setRefreshAndLoadMoreListener(this);
            rvStore.setAdapter(adapterMyGitfStore);
        }
        adapterMyGitfStore.setList(list);
        adapterMyGitfStore.notifyDataSetChanged();
        rvStore.reSetStatus();
    }

    @Override
    public void finishActivity() {
        finish();
    }


    @Override
    public void onRefresh() {
        page = 1;
        fragmentStorePresenter.getStoreList(page, page_num, sortPrice);
    }

    @Override
    public void onLoadMore() {
        page++;
        fragmentStorePresenter.getStoreList(page, page_num, sortPrice);
    }

    @OnClick({R.id.tv_toobar_right, R.id.iv_toobar_back, R.id.ll_indiana, R.id.ll_trading})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
            case R.id.tv_toobar_right://按价格排序
                if (sortPrice) {
                    sortPrice = false;
                } else {
                    sortPrice = true;
                }
                initToolbar();//更新标题栏信息
                fragmentStorePresenter.getStoreList(page, page_num, sortPrice);
                break;

            case R.id.ll_indiana://一元夺宝
                T.show(this, "此功能暂未开放", 0);
                break;
            case R.id.ll_trading://二手交易 旧物拍卖
                T.show(this, "此功能暂未开放", 0);
                break;
        }
    }

    @Override
    public void onItemClickListener(int position) {
        if ("flag".equals(flag)) {
            FragmentStoreBean.DataBean dataBean = list.get(position);
            Intent intent = new Intent(MyGiftsStoresActivity.this, StoreDetailsActivity.class);
            intent.putExtra("dataBean", dataBean);
            startActivityForResult(intent, 1);
        } else {
            FragmentStoreBean.DataBean dataBean = list.get(position);
            LaunchDateActivity.mGiftId = dataBean.gift_id;
            setResult(RESULT_OK);
            finish();
        }

    }

    @Override
    public void showLod(String msg) {
        showLoad(MyGiftsStoresActivity.this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(MyGiftsStoresActivity.this, msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                finish();
            }

        }
    }
}
